package org.example.tip.controller;
import org.example.tip.entity.Tip;
import org.example.tip.entity.vo.TipVO;
import org.example.tip.service.TipService;
import org.example.entity.Base.Result;
import org.example.util.FileSetUtil;
import org.example.util.JWTUtil;
import org.example.util.OssUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import jakarta.servlet.http.HttpServletRequest;
import java.io.File;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/tip")
public class TipController {
    private final TipService tipService;

    public TipController(TipService tipService) {
        this.tipService = tipService;
    }

    @PostMapping("/alltip")
    public Result<PageInfo> alltip(@RequestParam(name="PageNumber", defaultValue = "1")int PageNumber, @RequestParam(name="PageSize", defaultValue = "5")int PageSize){
        System.out.println(PageNumber+"|||||"+PageSize);
        return Result.success(tipService.alltip(PageNumber,PageSize));
    }
    @PostMapping("/addtip")
    public Result<Tip> addtip(HttpServletRequest request, String TipTitel, String TipText, MultipartFile TipPictrue, String TipTime, String TipUserE, int TipTypeId, String TipUserN){
        TipTime=(LocalDateTime.now()).toString();
        String imageurl="";
        if (TipPictrue!=null){
            File file = FileSetUtil.saveFile(TipPictrue, "./external-resources/", LocalTime.now().toString());
            imageurl= OssUtil.uploadImage(file.getName());
        }
        //获取令牌
        String token=request.getHeader("Authorization");
            Map<String, Object> c= JWTUtil.parseToken(token);
        TipUserN=(String)(c.get("Name"));
        TipUserE=(String)(c.get("Email"));
        Tip t=new Tip(null,TipTitel,TipText,imageurl,0,0,0,TipTime,TipUserE,1,TipTypeId,TipUserN);
        tipService.addtip(t);
        return Result.success(t);
    }
    @GetMapping("/gettip")
    public Result<TipVO> gettip(Integer id){
        return Result.success(tipService.gettip(id));
    }
    @GetMapping("/getMyGoodTipId")
    public Result<List<Integer>> getMyGoodTipId(HttpServletRequest request){
        String token=request.getHeader("Authorization");
        Map<String, Object> c= JWTUtil.parseToken(token);
        String UserEmail=(String)(c.get("Email"));
        return Result.success(tipService.getMyGoodTipId(UserEmail));
    }
    @PostMapping("/getMyTip")
    public Result<PageInfo> getMyTip(HttpServletRequest request
            ,@RequestParam(name="PageNumber", defaultValue = "1")int PageNumber,
            @RequestParam(name="PageSize", defaultValue = "5")int PageSize){
        String token=request.getHeader("Authorization");
        Map<String, Object> c= JWTUtil.parseToken(token);
        String UserEmail=(String)(c.get("Email"));
        return Result.success(tipService.getMyTip(UserEmail,PageNumber,PageSize));
    }
    @PostMapping("/userGoods")
    public Result userGoods(HttpServletRequest request,Integer id){
        String token=request.getHeader("Authorization");
        Map<String, Object> c= JWTUtil.parseToken(token);
        String UserEmail=(String)(c.get("Email"));
        tipService.userGoods(UserEmail,id);
        return Result.success();
    }
    @PostMapping("/cancelUserGoods")
    public Result cancelUserGoods(HttpServletRequest request,Integer id){
        String token=request.getHeader("Authorization");
        Map<String, Object> c= JWTUtil.parseToken(token);
        String UserEmail=(String)(c.get("Email"));
        tipService.cancelUserGoods(UserEmail,id);
        return Result.success();
    }
    @PostMapping("/getMyGoodsTip")
    public Result<PageInfo> getMyGoodsTip(HttpServletRequest request
            ,@RequestParam(name="PageNumber", defaultValue = "1")int PageNumber,
            @RequestParam(name="PageSize", defaultValue = "5")int PageSize){
        String token=request.getHeader("Authorization");
        Map<String, Object> c= JWTUtil.parseToken(token);
        String UserEmail=(String)(c.get("Email"));
        return Result.success(tipService.getMyGoodsTip(UserEmail,PageNumber,PageSize));
    }
    @PostMapping("/searchTip")
    public Result<PageInfo> searchTip(String all,
                                         @RequestParam(name="PageNumber", defaultValue = "1")int PageNumber,
                                         @RequestParam(name="PageSize", defaultValue = "5")int PageSize){
        System.out.println(all);
        return Result.success(tipService.searchTip(all,PageNumber,PageSize));
    }
}
